#import "AikitPlugin.h"
#import "FlutterPluginStream.h"
#import "param/IFlyIVWConfig.h"
#import "service/IFlyVoiceWakeupDelegate.h"
#import "service/IFlyIVWService.h"
#import "Definition.h"

#import "AIKIT/AiHelper.h"
#import "AIKIT/ILibrary.h"
#import "AIKIT/AIKITParameters.h"
#import "AIKIT/AiHandle.h"
#import "AIKIT/AiHelper.h"
#import "AIKIT/AIKITInputData.h"
#import "AIKIT/AIKITAudioBuilder.h"
#import "AIKIT/AIKITCustomData.h"

@interface AikitPlugin ()<IFlyVoiceWakeupDelegate, UITextFieldDelegate>

@property (nonatomic, strong) IFlyIVWConfig *ivwConfig;

@property (nonatomic, strong) IFlyIVWService *ivwService;

@end

@implementation AikitPlugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    FlutterMethodChannel *channel = [FlutterMethodChannel
            methodChannelWithName:@"aikit_plugin"
                  binaryMessenger:[registrar messenger]];
    AikitPlugin *instance = [[AikitPlugin alloc] init];
    [registrar addMethodCallDelegate:instance channel:channel];

    FlutterEventChannel *eventChanel = [FlutterEventChannel eventChannelWithName:@"aikit_plugin_stream" binaryMessenger:[registrar messenger]];
    [eventChanel setStreamHandler:[[FlutterPluginStream sharedInstance] streamHandler]];
}

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
    if ([@"getPlatformVersion" isEqualToString:call.method]) {
        result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
    }else if ([@"initSDK" isEqualToString:call.method]){
        [self defaultConfig : call.arguments];
    }else if ([@"startRecord" isEqualToString:call.method]){
        NSString *keyword = call.arguments[@"keyword"];
        self.ivwConfig = [IFlyIVWConfig sharedInstance];
        self.ivwConfig.wakeupWords = keyword;
        self.ivwService.config = _ivwConfig;
        [self.ivwService initEngine:AIKIT_ABILITYID_IVW];
        BOOL ret = [self.ivwService startListening];
        if (ret) {
            [IFlyIVWConfig sharedInstance].wakeupWordsLockedFlag = YES;
        }
                
    } else if ([@"stopRecord" isEqualToString:call.method]){
        [self.ivwService stopListening];
    } else {
        result(FlutterMethodNotImplemented);
    }
}

// SDK初始化
- (void) defaultConfig:(NSDictionary*) args{
    self.ivwService = [IFlyIVWService shareInstance];
    self.ivwService.delegate = self;
    dispatch_async(dispatch_queue_create("initSDK", DISPATCH_QUEUE_PRIORITY_DEFAULT), ^{
        [self.ivwService initSDK:args ];
        NSMutableDictionary *resdic = [NSMutableDictionary dictionaryWithCapacity:1];
        [resdic setObject: [NSNumber numberWithBool:YES] forKey:@"success"];
        [resdic setObject:@"" forKey:@"result"];
        [resdic setObject:@"0" forKey:@"code"];
        [resdic setObject:@"AUTH" forKey:@"type"];
        [[FlutterPluginStream sharedInstance] streamHandler].eventSink(resdic);
    });
    
}

#pragma mark - IFlyVoiceWakeupDelegate

- (void)onBeginOfSpeech {
    log(@"%s",__func__);
}

- (void)onEndOfSpeech {
    log(@"%s",__func__);
}

- (void)onVolumeChanged:(NSUInteger)volume {
    log(@"%s",@"%lu");
}

- (void)onCompletedWithErrorCode:(int)errorCode {
    if (errorCode) {
        log(@"errorCode:%i", errorCode);
    }
    else {
        log(@"唤醒结束!");
        
    }
}

- (void)onResultStr:(NSString *)resultStr {
    log(@"识别成功");
    log(@"%@",resultStr);
    NSMutableDictionary *resdic = [NSMutableDictionary dictionaryWithCapacity:1];
    [resdic setObject: [NSNumber numberWithBool:YES] forKey:@"success"];
    [resdic setObject:resultStr forKey:@"result"];
    [resdic setObject:@"0" forKey:@"code"];
    [resdic setObject:@"wake_up" forKey:@"type"];
    [[FlutterPluginStream sharedInstance] streamHandler].eventSink(resdic);
}

@end
